<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="200"
			label-align="left">
			<u-form-item label="费用申请单号" v-if="judgeShow('tg003')">
				<u-input v-model="dataForm.tg003" disabled placeholder="提交保存后系统自动生成"></u-input>
			</u-form-item>
			<u-form-item label="申请人" v-if="judgeShow('tg002')">
				<jnpf-user-select v-model="dataForm.tg002" @input="changeName" />
			</u-form-item>
			<u-form-item label="部门" v-if="judgeShow('tg011')">
				{{dataForm.tg013}}
				<!-- <jnpf-com-select v-model="dataForm.tg011" @hangledepData="hangledepData" /> -->
			</u-form-item>
			<u-form-item label="发生日期" v-if="judgeShow('tg007')">
				<lys-date v-if="showte005" type="1" @cancleTime="showte005 = false"
					@submitTime="submitte005"></lys-date>
				<view style="display: flex;justify-content: space-between;width: 100%;">
					<view>
						{{dataForm.tg007}}
					</view>
					<view>
						<u-icon name="arrow-down-fill" color='#ccc' @click="showte005=true"></u-icon>
					</view>
				</view>
			</u-form-item>
			<u-form-item label="费用预算合计" v-if="judgeShow('tg012')">
				<u-input v-model="dataForm.tg012" disabled></u-input>
			</u-form-item>
			<u-form-item label="备注" v-if="judgeShow('tg009')">
				<u-input v-model="dataForm.tg009"></u-input>
			</u-form-item>
			<u-form-item label="附件" type="textarea" v-if="judgeShow('tg010')">
				<jnpf-file v-model="dataForm.tg010" :disabled="judgeWrite('tg010')" />
			</u-form-item>
			<view class="jnpf-table" style="margin-top: 24rpx">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx" v-for="(item, index) in dataForm.oastkList">
					<u-form-item label="序号" style="background-color: aqua">
						{{ index + 1 }}
					</u-form-item>

					<u-form-item label="费用类型" v-if="judgeShow('oastkList-tk003')">
						<view style="display: flex;justify-content: space-between;width: 100%;">
							<view>
								{{item.tk003Name}}
							</view>
							<view>
								<u-icon name="arrow-down-fill" color='#ccc' @click="chioceOastdBox(index)"></u-icon>
							</view>
						</view>
					</u-form-item>
					<u-form-item label="预算金额" v-if="judgeShow('oastkList-tk004')">
						<u-input v-model="item.tk004" :disabled="
                      judgeWrite('oastkList') || judgeWrite('oastkList-tk004')
                    "></u-input>
					</u-form-item>
					<u-form-item label="备注" v-if="judgeShow('oastkList-tk005')">
						<u-input v-model="item.tk005" :disabled="
                      judgeWrite('oastkList') || judgeWrite('oastkList-tk005')
                    "></u-input>
					</u-form-item>
					<u-button type="error" plain :ripple="true" @click="delPlan(index)">删除</u-button>
				</view>
			</view>
			<u-button type="primary" @click="addHandleOastfEntityList()"
				v-if="!setting.readonly && !judgeWrite('oastkList')">新增</u-button>
		</u-form>
		<u-toast ref="uToast" />
		<u-popup v-model="popupshow" mode="bottom" height="50vh">
			<view class="content">
				<scroll-view scroll-y="true" style="height: 800rpx;">
					<zsf-table :radio='true' @getRow='getSelectData' ref="product" :title='productionQueueTitle'
						:data="tableList"></zsf-table>
				</scroll-view>
				<view class="confrim-btn">
					<u-button @click="close">确定</u-button>
				</view>
			</view>
		</u-popup>


	</view>
</template>

<script>
	import comMixin from "../mixin";
	import {
		getGoodsList,
		getCustomerList,
		Info,

	} from "@/api/apply/order";
	import {
		postOastg,
		putOaste,
		BomcbListInit,
		getOastgBudgetNo,
		getOastdList
	} from "@/api/workFlow/flowBefore";
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo,
		getUserAll
	} from "@/api/common";
	import {
		UserSettingInfo
	} from "@/api/user"
	export default {
		mixins: [comMixin],
		watch: {
			'dataForm.oastkList': {
				handler(newVal) {
					let total = 0;
					newVal.forEach(item => {
						if (item.tk004) {
							total += parseFloat(item.tk004);
						}
					});
					this.dataForm.tg012 = total;
				},
				deep: true // 深度监听数组内部的变化
			}
		},
		data() {
			return {
				show: false,
				popupshow: false,
				showtf003: false,
				showte005: false,
				showte006: false,
				showte008: false,
				showte009: false,
				productionQueueTitle: [{
						title: "序号",
						key: "sid",
						width: '80',
					},
					{
						title: "费用类型编号",
						key: "td001",
						width: '80',
					},
					{
						title: "费用类型名称",
						key: "td002",
						width: '80',
					},

				],
				dataForm: {
					tg002: '',
					tg003: '',
					tg007: '',
					tg009: "",
					tg010: [],
					tg011: null,
					tg012: null,
					tg013: null,
					oastkList: [],
					flowId: null,
					flowState: 0 //提交0 保存1
				},
				id: "",
				tableList: [],
				tk003: '',
				tk003Name: '',
				index: '',
				lastItemId: '',
				lastItemName: '',


			};
		},
		methods: {
			getUserInfo() {
				UserSettingInfo().then((res) => {
					this.dataForm.tg002 = res.data.id; //用户
					this.dataForm.tg011 = res.data.organizeId; //部门
					this.dataForm.tg013 = res.data.department; //部门名称
				})
			},
			changeName(id) {
				getUserAll().then(res => {
					console.log('res===>', res.data);
					let item = res.data.filter(o => o.id === id)[0]
					console.log('item===>', item);
					this.dataForm.tg011 = item.departmentId; //部门编号
					this.dataForm.tg013 = item.department; //部门名称
				})
			},
			submitte005(time, type, timeInt) {
				let tg007 = time.split("-");
				this.dataForm.tg007 = tg007[0] + tg007[1] + tg007[2]
				this.showte005 = false;
			},
			submitte006(time, type, timeInt) {
				this.dataForm.te006 = time
				this.showte006 = false;
			},
			submitte008(time, type, timeInt) {
				this.dataForm.te008 = time
				this.showte008 = false;
			},
			submitte009(time, type, timeInt) {
				this.dataForm.te009 = time
				this.showte009 = false;
			},
			submittf003(time, type, timeInt) {
				this.dataForm.oastkList[this.index].tf003 = time
				this.showtf003 = false;
			},

			Showtf003(index) {
				this.index = index;
				this.showtf003 = true;
			},
			closingDateChange() {
				var year = this.dataForm.te008.substring(0, 4); // 获取预算起始月的年份
				var month = this.dataForm.te008.substring(5, 7); // 获取预算起始月的月份
				const nowDate = this.dataForm.te008; //将预算起始月赋值给变量nowDate
				//预算类型为年度
				if (this.dataForm.te007 == 3) {
					//初始化预算截至月
					var m = Number(month) - 1;
					if (m == 0) {
						m = 12;
					} else {
						var y = Number(year) + 1;
						year = y.toString();
					}
					if (m >= 1 && m <= 9) {
						m = "0" + m;
					}
					month = m.toString();
					var endDate = year + '-' + month;
					this.dataForm.te009 = endDate;
				}
				//预算类型为季度
				else if (this.dataForm.te007 == 2) {
					//初始化预算截至月
					var m = Number(month) + 2;
					if (m > 12) {
						m = m - 12;
						var y = Number(year) + 1;
						year = y.toString();
					}
					if (m >= 1 && m <= 9) {
						m = "0" + m;
					}
					month = m.toString();
					var endDate = year + '-' + month;
					this.dataForm.te009 = endDate;
				}
				//预算类型为月度
				else if (this.dataForm.te007 == 1) {
					this.dataForm.te009 = this.dataForm.te008;
				}
			},
			getSelectData(e) {
				this.tk003 = e.data.td001;
				this.tk003Name = e.data.td002;
			},
			// 删除
			delPlan(index) {
				uni.showModal({
					title: "提示",
					content: "您确定要删除当前数据吗？",
					success: (res) => {
						if (res.confirm) {
							this.dataForm.oastkList.splice(index, 1);
							this.$u.toast(`删除了第${index+1}条`);
						}
					},
				});

			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.dataForm.oastkList[index].show = true;
				this.dataForm.oastkList.map((val, idx) => {
					if (index != idx) this.dataForm.oastkList[idx].show = false;
				})
			},
			close() {
				this.popupshow = false;
				this.dataForm.oastkList[this.index].tk003Name = this.tk003Name
				this.dataForm.oastkList[this.index].tk003 = this.tk003
			},
			hangledepData(data) {
				if (data) {
					let length = data.ids[0].length
					let ids = data.ids[0]
					let str = data.name[0];
					let parts = str.split('/');
					// 检查数组是否为空，然后安全地获取最后一项
					this.lastItemId = length > 0 ? ids[length - 1] : null;
					this.lastItemName = parts.length === 1 ? parts[0] : parts[parts.length - 1];
				}
			},
			chioceOastdBox(index) {
				if (!this.dataForm.tg011) {
					this.$refs.uToast.show({
						title: '部门不能为空！',
						type: 'error',
					})
					return
				}

				this.index = index; //当前下标
				//报销部门
				var queryParam = {
					td006: this.dataForm.tg011,
					currentPage: 1,
					pageSize: 500,
					sort: 'asc',
					sidx: 'TD001',
				};
				getOastdList(queryParam).then(res => {
					this.popupshow = true
					this.tableList = res.data.list.map((item, index) => {
						return {
							sid: index + 1,
							...item
						}
					})

				})
			},

			addHandleOastfEntityList() {
				let item = {
					tk003: null,
					tk003Name: null,
					tk004: 0,
					tk005: null,
				};
				if (!this.dataForm.tg011) {
					this.$refs.uToast.show({
						title: '部门不能为空！',
						type: 'error',
					})
					return
				}
				this.dataForm.oastkList.push(item);
			},
			getInfoFn(data) {
				this.dataForm.flowId = data.flowId
				getOastgBudgetNo().then((res) => {
					if (res.code == 200) {
						// this.dataForm.tg003 = res.data;
						this.dataForm.tg007 = this.jnpf.getCurrentDate('YYYYMMDD');
						this.getUserInfo()
					}
				});
			},
			hasEmptySta(array) {
				// 首先检查数组是否为空
				if (array.length === 0) {
					return true; // 如果数组为空，直接返回false
				}
				// 然后检查数组中是否有对象的sta属性为空
				return array.some(item => item.tk003 === null || item.tk003 === undefined || item.tk003 === '');
			},
			selfSubmit(eventType) {
				console.log('this.dataForm.oastkList', this.dataForm.oastkList);
				let stauts = this.hasEmptySta(this.dataForm.oastkList)
				console.log('stauts', stauts);
				if (stauts) {
					console.log('存在为空');
					this.$refs.uToast.show({
						title: '费用类型不能为空!',
						type: 'error',
					})
					return

				}
				this.dataForm.status = eventType === "submit" ? 0 : 1;
				uni.showModal({
					title: "提示",
					content: "您确定要提交当前流程吗？",
					success: (res) => {
						if (res.confirm) {
							this.HandleSubmit(eventType);
						}
					},
				});


			},
			HandleSubmit(eventType) {
				this.dataForm.flowState = eventType == "submit" ? 0 : 1;
				console.log('this.dataForm', this.dataForm);
				postOastg(this.dataForm).then((res) => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit("refresh");
								uni.navigateBack();
							}, 1500);
						},
					});

				});
			},

		},
	};
</script>